
//
//  EmoticonViewController.m
//  BiaoQing
//
//  Created by 张帅 on 15/10/12.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "EmoticonViewController.h"
#import "FSEmoticonViewModel.h"
#import "FSEmoticonPackageModel.h"

#import "FSEmoticonModel.h"
@interface EmoticonViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property(nonatomic,strong) UICollectionView *collectionView;


@property(nonatomic,strong) UIToolbar *toolbar;

@property(nonatomic,strong) FSEmoticonViewModel *viewModel;


@end


static NSString *EmoticonCellID  =@"EmoticonCellID";

@implementation EmoticonViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    if (self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
         [self setupUI];
    }
    return self;
}

-(void)clickItem:(UIBarButtonItem*)item{
    
    NSLog(@"item--%ld",(long)item.tag);
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:item.tag];
    
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
   
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.viewModel loadData];
    self.view.backgroundColor =[UIColor whiteColor];
    
   
}



-(void)setupUI{
    
    // 1. 添加控件
    
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.toolbar];
    
    self.collectionView.backgroundColor =[UIColor whiteColor];
    NSDictionary *dict=@{@"cv":self.collectionView,@"tv":self.toolbar};
    
    self.toolbar.translatesAutoresizingMaskIntoConstraints =NO;
    self.collectionView.translatesAutoresizingMaskIntoConstraints =NO;
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[cv]-0-|" options:0 metrics:nil views:dict]];
    
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tv]-0-|" options:0 metrics:nil views:dict]];
    
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[cv]-[tv(44)]-0-|" options:0 metrics:nil views:dict]];
    
    
    [self prepareCollectionView];
    [self prepareToolbar];;
}


-(void)prepareToolbar{
    
    self.toolbar.tintColor =[UIColor darkGrayColor];
    
    
    NSMutableArray *items =[NSMutableArray array];
    
    // 通常用 tag 来区别 toolbar 上的按钮，一组相近，而且是顺序的操作
    
    NSInteger index = 0;
    
    for ( FSEmoticonPackageModel *p in self.viewModel.emoticonPackages) {
        
        UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithTitle:p.group_name_cn style:UIBarButtonItemStylePlain target:self action:@selector(clickItem:)];
       
        barButton.tag =index++;
        
        [items addObject:barButton];
        
        UIBarButtonItem *  flexibleSpace =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        [items addObject:flexibleSpace];
        
        
    }
    [items removeLastObject];
    
    self.toolbar.items =items;
    
}




-(void)prepareCollectionView{
    
    [self.collectionView registerClass:[EmoticonCell class] forCellWithReuseIdentifier:EmoticonCellID];
    
    self.collectionView.dataSource =self;
    
    self.collectionView.delegate=self;
    
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{


    return  self.viewModel.emoticonPackages.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
   
    return [self.viewModel.emoticonPackages[section]emoticons].count;
    
}




-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    EmoticonCell*cell =[collectionView dequeueReusableCellWithReuseIdentifier:EmoticonCellID forIndexPath:indexPath];
    
//    cell.emoticon=[self.viewModel.emoticonPackages[indexPath.section] emoticons][indexPath.item];
    
    cell.emoticon =[self.viewModel emoticon:indexPath];
    return cell;
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


//    FSEmoticonModel * model =[self.viewModel.emoticonPackages[indexPath.section] emoticons][indexPath.row];
    
    if (self.selectedEmoticonCallBack) {
        self.selectedEmoticonCallBack([self.viewModel emoticon:indexPath]);
         // 添加最近表情符
    }
     [self.viewModel favorite:indexPath];
    
    
    
}


-(UIToolbar *)toolbar{
    if (_toolbar==nil) {
        _toolbar=[[UIToolbar alloc] init];
    }
    return _toolbar;
}

-(UICollectionView *)collectionView{
    if (_collectionView==nil) {
        
        
        _collectionView=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[EmoticonLayout alloc] init]];
        
    }
    
    return _collectionView;
    
}


-(FSEmoticonViewModel *)viewModel{

    if (_viewModel==nil) {
        
//        _viewModel=[[FSEmoticonViewModel alloc] init];
        
        _viewModel = [FSEmoticonViewModel shareEmoticonViewModel];
    }
    return _viewModel;
}


@end


@interface EmoticonLayout()


@end

@implementation EmoticonLayout

-(void)prepareLayout{
    
    [super prepareLayout];
    
    CGFloat w = self.collectionView.bounds.size.width/7;
    
    CGFloat margin =( self.collectionView.bounds.size.height- 3*w)*0.499;
    
    
    self.itemSize = CGSizeMake(w, w);
    self.minimumInteritemSpacing=0;
    
    self.minimumLineSpacing=0;
    
    self.sectionInset = UIEdgeInsetsMake(margin, 0, margin, 0);
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView.pagingEnabled =YES;
    
    self.collectionView.showsHorizontalScrollIndicator=NO;
    self.collectionView.bounces=NO;
    
    
}


@end


@interface EmoticonCell ()

@end

@implementation EmoticonCell

-(void)setEmoticon:(FSEmoticonModel *)emoticon{

    _emoticon=emoticon;
    
    // 以下两个清空的动作，能够解决重用的问题
    // 1. 图片 - 如果没有会清空图片
    
    [self.emoticonButton setImage:[UIImage imageWithContentsOfFile:emoticon.pngPath] forState:(UIControlStateNormal)];
    
         // 2. emoji － 如果没有会清空文字
    
    [self.emoticonButton setTitle:emoticon.emoji forState:(UIControlStateNormal)];
    
    if (emoticon.isDeleteButton) {
        
        [self.emoticonButton setImage:[UIImage imageNamed:@"compose_emotion_delete"] forState:(UIControlStateNormal)];
        
        [self.emoticonButton setImage:[UIImage imageNamed:@"compose_emotion_delete_highlighted"] forState:(UIControlStateHighlighted)];
        
        
        
    }
    
    

}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        
        [self setupUI];
        
    }
    
    return self;
}


-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if (self=[super initWithCoder:aDecoder]) {
        [self setupUI];
        
    }
    
    return self;
}
-(void)setupUI{
    [self.contentView addSubview:self.emoticonButton];
    
    self.emoticonButton.backgroundColor=[UIColor whiteColor];
    
    self.emoticonButton.frame =CGRectInset(self.contentView.bounds, 4, 4);
    
    self.emoticonButton.userInteractionEnabled =NO;
    
}

-(UIButton *)emoticonButton{
    
    if (_emoticonButton==nil) {
        _emoticonButton=[[UIButton alloc] init];
    }
    return _emoticonButton;
}

@end
